package com.ysd.dao.Impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Statement;
import com.ysd.entity.Product;
import com.ysd.util.DBManager;

public class ChanPinMoHuDaoImpl {
	//对产品进行模糊查询
	public ArrayList<Product> getChanPinMoHu(String name){//什么情况下有参数？一般看sql语句有没有条件
   	 ArrayList<Product> mohuList=new ArrayList<Product>();
   	 Connection conn=DBManager.getConnection();//建立连接
   	 Statement st=null;
   	 ResultSet rs=null;
   	 
   	 try {
   		 st=(Statement) conn.createStatement();//创建statement对象
   		 rs=st.executeQuery("select * from product where productName like '%"+name+"%'");
   		 while(rs.next()) {//遍历结果集
   			 int productId=rs.getInt("productId");
   			 String productName=rs.getString("productName");
   			 String productImg=rs.getString("productImg");
   			 Product product=new Product(productId,productName,productImg);
   			mohuList.add(product);
   		 }
   		 return mohuList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {//关闭流
			DBManager.closeAll(conn, st, rs);
		}
   	 
    }
}
